/**
 * 
 */
package com.easidea.tim.service.temp;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.temp.TempInventoryDao;
import com.easidea.tim.dao.temp.TempInventoryDetailDao;
import com.easidea.tim.entity.temp.TempInventory;
import com.easidea.tim.entity.temp.TempInventoryDetail;
import com.easidea.tim.util.Page;

/**
 * @author Harrison
 *
 */
public class TempInventoryService extends EntityService<TempInventory,TempInventoryDao> {

	private TempInventoryDao tempInventoryDao;
	private TempInventoryDetailDao tempInventoryDetailDao;
	
	public boolean saveTempInventory(TempInventory ti){
		boolean rst = false;
		if(ti != null){
			Criteria cte = tempInventoryDao.createUnDeletedCriteria();
			cte.add(Restrictions.eq("productName", ti.getProductName()));
			cte.add(Restrictions.eq("colorCode", ti.getColorCode()));
			cte.add(Restrictions.eq("sampleFlag",ti.getSampleFlag()));
			TempInventory newTi = (TempInventory)cte.uniqueResult();
			if(newTi != null && newTi.getInventoryDetailList().size()> 0){
				Double totalMeterNum = newTi.getTotalMeterNum();
				Double totalPiNum = newTi.getTotalPiNum();
				for (TempInventoryDetail tid : ti.getInventoryDetailList()) {
					if(tid != null){
						tid.setId(null);
						tid.setTempInventory(newTi);
						tempInventoryDetailDao.save(tid);
						totalMeterNum = totalMeterNum + tid.getMeterNum();
						totalPiNum++;
					}
				}
				
				newTi.setTotalMeterNum(totalMeterNum);
				newTi.setTotalPiNum(totalPiNum);
				tempInventoryDao.merge(newTi);
			}else{
				// 保存
				ti.setId(null);
				tempInventoryDao.save(ti);
				for (TempInventoryDetail tid : ti.getInventoryDetailList()) {
					if(tid != null){
						tid.setId(null);
						tid.setTempInventory(ti);
						tempInventoryDetailDao.save(tid);
					}
				}
			}
			rst = true;
		}
		return rst;
	}
	
	public boolean updateDetailMeterNum(String name, String colorCode, Boolean sampleFlag, Integer index, Double value, Date inventoryDate){
		Criteria cte = tempInventoryDao.createUnDeletedCriteria();
		cte.add(Restrictions.eq("productName", name));
		cte.add(Restrictions.eq("colorCode", colorCode));
		cte.add(Restrictions.eq("sampleFlag", sampleFlag));
		cte.add(Restrictions.eq("inventoryDate", inventoryDate));
		TempInventory ti = (TempInventory)cte.uniqueResult();
		if(ti != null && ti.getInventoryDetailList() != null && ti.getInventoryDetailList().size()>0){
			TempInventoryDetail tid = ti.getInventoryDetailList().get(index);
			ti.setTotalMeterNum(ti.getTotalMeterNum() - tid.getMeterNum() + value);
			tid.setMeterNum(value);
			return true;
		}
		return false;
	}
	
	
	
	public Page getTempInventoryList(String name, String colorCode, Boolean sampleFlag, String operator, Date startDate, Date endDate, Page page){
		Criteria rowCountsCte = genCriteria(name, colorCode, sampleFlag, operator,
				startDate, endDate);
		rowCountsCte.setProjection(Projections.count("id"));
		Integer totalCounts = (Integer) rowCountsCte.uniqueResult();
		
		Criteria cte = genCriteria(name, colorCode, sampleFlag, operator,
				startDate, endDate);
		int index = Page.getStartOfPage(page.getCurrentPageNo(), page.getPageSize());
		cte.addOrder(Order.desc("inventoryDate"));
		cte.setFirstResult(index);
		cte.setMaxResults(page.getPageSize());
		List<TempInventory> rtl = (List<TempInventory>) cte.list();

		Page resultPage = new Page(index, totalCounts, page.getPageSize(), rtl);
		resultPage.setCurrentPageNo(page.getCurrentPageNo());
		return resultPage;
	}

	private Criteria genCriteria(String name, String colorCode,
			Boolean sampleFlag, String operator, Date startDate, Date endDate) {
		Criteria cte = tempInventoryDao.createUnDeletedCriteria();
		if(name != null && name.length() >0)cte.add(Restrictions.ilike("productName", "%"+name+"%"));
		if(colorCode != null && colorCode.length() > 0)cte.add(Restrictions.ilike("colorCode", "%"+colorCode+"%"));
		if(sampleFlag != null) cte.add(Restrictions.eq("sampleFlag",sampleFlag));
		if(operator != null && operator.length()>0) cte.add(Restrictions.eq("creator",operator));
		if(startDate != null && endDate != null){
			switch (startDate.compareTo(endDate)) {
			case 0:
				cte.add(Restrictions.eq("inventoryDate", startDate));
				break;
				
			case -1:
				cte.add(Restrictions.between("inventoryDate", startDate, endDate));
				break;

			default:
				break;
			}
		}
		return cte;
	}
	
	public TempInventory getInventoryDetail(Long id){
		TempInventory ti = null;
		if(id != null){
			ti = tempInventoryDao.get(id);
			ti.getInventoryDetailList().size();
		}
		return ti;
	}
	
	
	
	public Boolean removeDust(Long id){
		if(id != null){
			return tempInventoryDao.deleteTempInventory(id);
		}else
			return false;
	}
	
	/* (non-Javadoc)
	 * @see org.springside.core.service.EntityService#getEntityDao()
	 */
	@Override
	public TempInventoryDao getEntityDao() {
		return tempInventoryDao;
	}

	/**
	 * @return the tempInventoryDao
	 */
	public TempInventoryDao getTempInventoryDao() {
		return tempInventoryDao;
	}

	/**
	 * @param tempInventoryDao the tempInventoryDao to set
	 */
	public void setTempInventoryDao(TempInventoryDao tempInventoryDao) {
		this.tempInventoryDao = tempInventoryDao;
	}

	/**
	 * @return the tempInventoryDetailDao
	 */
	public TempInventoryDetailDao getTempInventoryDetailDao() {
		return tempInventoryDetailDao;
	}

	/**
	 * @param tempInventoryDetailDao the tempInventoryDetailDao to set
	 */
	public void setTempInventoryDetailDao(
			TempInventoryDetailDao tempInventoryDetailDao) {
		this.tempInventoryDetailDao = tempInventoryDetailDao;
	}
	
	
}
